package com.zhongxi.guoyuan.view.rewardCoin;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.fragment.hmfragment.AllMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.HMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.OneMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.TwoMFragment;
import com.zhongxi.guoyuan.utils.CommonTool;

/**
 * Created by Administrator on 2018/3/2.
 */

public class SearchActivity extends BaseActivity {

    private EditText et_coin_shop;
    private TextView[] tabs;
    private AllMFragment zeroFragment;
    private HMFragment firstFragment;
    private TwoMFragment secondFragment;
    private OneMFragment thirdFragment;
    private Fragment[] fragments;
    private int index;
    private int currentIndex;
    private LinearLayout mLl_search;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_search);
        initTitle();
        tvTitle.setText("线上赚豆超市");
        et_coin_shop = (EditText) findViewById(R.id.et_coin_shop);
        mLl_search = (LinearLayout) findViewById(R.id.ll_search);
    }

    @Override
    protected void initData() {
        super.initData();
        tabs = new TextView[4];
        tabs[0] = (TextView) findViewById(R.id.tv0);
        tabs[1] = (TextView) findViewById(R.id.tv1);
        tabs[2] = (TextView) findViewById(R.id.tv2);
        tabs[3] = (TextView) findViewById(R.id.tv3);
        zeroFragment = new AllMFragment();
        firstFragment = new HMFragment();
        secondFragment = new TwoMFragment();
        thirdFragment = new OneMFragment();
        fragments = new Fragment[]{zeroFragment, firstFragment, secondFragment, thirdFragment};
        tabs[0].setSelected(true);
        getSupportFragmentManager().beginTransaction().add(R.id.ll_container, fragments[0]).commit();
        getRefreshFragment("");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mLl_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getRefreshFragment(et_coin_shop.getText().toString().trim());

                //隐藏软键盘
                CommonTool.hideSoft(et_coin_shop, SearchActivity.this);
            }
        });


        et_coin_shop.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() <= 0) {
                    switch (index) {
                        case 0:
                            zeroFragment.cleanList();
                            break;
                        case 1:
                            firstFragment.cleanList();
                            break;
                        case 2:
                            secondFragment.cleanList();
                            break;
                        case 3:
                            thirdFragment.cleanList();
                            break;
                    }
                }
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                index = 0;
                break;
            case R.id.tv1:
                index = 1;
                break;
            case R.id.tv2:
                index = 2;
                break;
            case R.id.tv3:
                index = 3;
                break;
        }
        refresh();
    }

    private void refresh() {
        if (index != currentIndex) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if (fragments[currentIndex].isAdded()) {
                trx.hide(fragments[currentIndex]);
            }
            if (!fragments[index].isAdded()) {
                trx.add(R.id.ll_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
        String mTrim = et_coin_shop.getText().toString().trim();
        if (!TextUtils.isEmpty(mTrim)) {
            getRefreshFragment(mTrim);
        }
    }

    private void getRefreshFragment(String trim) {
        switch (index) {
            case 0:
                zeroFragment.setPrimary("0", trim);
                break;
            case 1:
                firstFragment.setPrimary("0", trim);
                break;
            case 2:
                secondFragment.setPrimary("0", trim);
                break;
            case 3:
                thirdFragment.setPrimary("0", trim);
                break;
        }
    }
}
